package org.totoro.patterns.behavior.interpreter.demo;


/**
 * @author YHL
 * @version V1.0
 * @Description: 单个值运算
 * @date 2018-11-20
 */
public class OperatorPredicate<V> extends PredicateAbs<Boolean> {

    private IExpression expression;

    private Operator operator;

    private V value;

    public OperatorPredicate(Operator operator, V value) {
        this.operator = operator;
        this.value = value;
    }

    public OperatorPredicate(IExpression expression, Operator operator, V value) {
        this.expression = expression;
        this.operator = operator;
        this.value = value;
    }

    @Override
    public Boolean evaluate(Object context) {
        Object evaluate = expression.evaluate(context);
        boolean result = operator.evaluate(evaluate, value);
        return result;
    }

    @Override
    public String toString() {

        String tostring = value + " " + operator.name() + " " + expression.toString();

        return tostring;
    }
}
